#include "maininfor.h"
#include "virmachineinfor.h"
#include "securityinfor.h"
#include "normalinfor.h"
#include "fileinfor.h"
#include "emailinfor.h"
#include "deepinfor.h"
#include "../ThreadModel/thread_pool.h"

MainInfor* MainInfor::_instance=nullptr;

MainInfor* MainInfor::GetInstance(QObject* parent){
    if(MainInfor::_instance==nullptr)
        MainInfor::_instance=new MainInfor(parent);
    return MainInfor::_instance;
}

MainInfor::~MainInfor(){
    this->flag=false;

}

void MainInfor::SubmitInfor(Yzty::MODECODE mcode,Yzty::ITEMCODE icode,QVector<Yzty::BaceInfor*>&& infors){

    {
        std::lock_guard<std::mutex> glock(this->_mutex);

        //存入临时队列
        _tempq.push_back(QPair<Yzty::MODECODE,QPair<Yzty::ITEMCODE,QVector<Yzty::BaceInfor*>>>(mcode,
                                               QPair<Yzty::ITEMCODE,QVector<Yzty::BaceInfor*>>(icode,infors)));
    }

    this->_cond.notify_one();   //通知取消息
}

void MainInfor::Notify(){

    while(this->flag){

        std::unique_lock<std::mutex> qlock(this->_mutex);
        this->_cond.wait(qlock,[this](){
            return !flag||!this->_tempq.empty();
        });

        if(flag){
            QPair<Yzty::MODECODE,QPair<Yzty::ITEMCODE,QVector<Yzty::BaceInfor*>>> temp=_tempq.front();

            switch(Yzty::ToolCode(temp.first))
            {
                case Yzty::ToolCode::VirMachine:
                    VirMachineInfor::GetInstance()->SubmitInfor(temp.second.first,temp.second.second);
                    break;
                case Yzty::ToolCode::Security:
                    SecurityInfor::GetInstance()->SubmitInfor(temp.second.first,temp.second.second);
                    break;
                case Yzty::ToolCode::Report:
                    VirMachineInfor::GetInstance()->SubmitInfor(temp.second.first,temp.second.second);
                    break;
                case Yzty::ToolCode::Normal:
                    NormalInfor::GetInstance()->SubmitInfor(temp.second.first,temp.second.second);
                    break;
                case Yzty::ToolCode::Email:
                    EmailInfor::GetInstance()->SubmitInfor(temp.second.first,temp.second.second);
                    break;
                case Yzty::ToolCode::File:
                    FileInfor::GetInstance()->SubmitInfor(temp.second.first,temp.second.second);
                    break;
                case Yzty::ToolCode::Deep:
                    DeepInfor::GetInstance()->SubmitInfor(temp.second.first,temp.second.second);
                    break;
                default:
                    break;
            }
        }

    }
}

const QVector<Yzty::BaceInfor*>& MainInfor::GetItemInfor(Yzty::MODECODE mcode,Yzty::ITEMCODE icode){
    std::lock_guard<std::mutex> glock(this->_mutex);
    switch(Yzty::ToolCode(mcode))
    {
        case Yzty::ToolCode::VirMachine:
            return VirMachineInfor::GetInstance()->GetItemInfor(icode);
        case Yzty::ToolCode::Security:
            return SecurityInfor::GetInstance()->GetItemInfor(icode);
        case Yzty::ToolCode::Report:
            return SecurityInfor::GetInstance()->GetItemInfor(icode);
        case Yzty::ToolCode::Normal:
            return NormalInfor::GetInstance()->GetItemInfor(icode);
        case Yzty::ToolCode::Email:
            return EmailInfor::GetInstance()->GetItemInfor(icode);
        case Yzty::ToolCode::File:
            return FileInfor::GetInstance()->GetItemInfor(icode);
        case Yzty::ToolCode::Deep:
            return DeepInfor::GetInstance()->GetItemInfor(icode);
        default:
            return DeepInfor::GetInstance()->GetItemInfor(icode);
    }
}


MainInfor::MainInfor(QObject* parent):QObject (parent){
    this->flag=true;

    ThreadPool::GetInstance(10)->commit(&MainInfor::Notify,this);
}


Yzty::StatusBarInfor& MainInfor::GetStatusBarInfor(){
    return this->_statusInfor;
}
void MainInfor::SetStatusBarInfor(const Yzty::StatusBarInfor& infor){
    this->_statusInfor=infor;
}
